<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-4-27
 * Time: 下午2:07
 */

class message extends adminBase{
    function  __construct(){
        parent::__construct();
        $this->cms->setPageTitle("消息管理");
        $this->cms->setControlFile("admin/tpl/message/msgcontrol.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(10,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($page=1,$user_tel){
        $msg=MsgMode::init();
        $user=UserModule::init('user');
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        if(isset($user_tel)&&!empty($user_tel)){
            $user_id=$user->getIdByUsername($user_tel);
        }else{
            $user_id=null;
        }
        list($r['msg_info'],$msg_total)=$msg->getList($user_id,null,$countOfPage,$offset);
        $r['msg_info']=is_array($r['msg_info'])?$r['msg_info']:array();
        foreach($r['msg_info'] as &$v){
            $user_info=$user->getDetail($v['user_id']);
            $v['user_id']=$user_info['username'];
        }
        $r['pages']=new Page($page,$msg_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $this->cms->tableScene($r,"admin/tpl/message/message.php");
    }

    function sendTask(){
        $this->cms->setActionTitle("发送消息");
        $this->cms->formScene(array(),"admin/tpl/message/sendMsg.php");
    }

    function sendSubmitTask($msg_type,$user_type,$title,$content){
        $msg=MsgMode::init();
        $user=UserModule::init('user');

        switch($msg_type){
            case 1:$msg_type='system';break;
            case 2:$msg_type='shop';break;
            case 3:$msg_type='recommend';break;
        }
        switch($user_type) {
            case 1:
                $user_type = 'shop';
                break;
            case 2:
                $user_type = 'service';
                break;
            default :
                $user_type = null;
                break;
        }
        if($user_type){
            list($partUser,$part_total)=$user->getListByType(1000,0,$user_type);
            foreach($partUser as $v){
                $result=$msg->send($v['id'],$title,$content,$msg_type);
            }
        }else{
            $result=$msg->send(0,$title,$content,$msg_type);
        }
        $web=WebRouter::init();
        header("Location:".$web->getPage("message","index"));
    }
    

} 